﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Diagnostics;
using System.Windows;

namespace JiveMessenger.ViewModels
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class ViewModelDownloadProgress : ViewModelBase, IViewModelDownloadProgress
    {
        public RelayCommand<Point> UpdatePositionCommand { get; set; }

        private int _downloadPercent;
        public int DownloadPercent
        {
            get { return _downloadPercent; }
            set
            {
                if (_downloadPercent != value)
                {
                    Set(() => DownloadPercent, ref _downloadPercent, value);
                }
            }
        }

        private int _errorCode;
        public int ErrorCode
        {
            get { return _errorCode; }
            set
            {
                if (_errorCode != value)
                {
                    Set(() => ErrorCode, ref _errorCode, value);
                    RaisePropertyChanged("DownloadPercent");
                }
            }
        }


        private bool _showDownloadProgress;
        public bool ShowDownloadProgress
        {
            get { return _showDownloadProgress; }
            set
            {
                if (_showDownloadProgress != value)
                {
                    Set(() => ShowDownloadProgress, ref _showDownloadProgress, value);
                }
            }
        }

        private double _left;
        public double Left
        {
            get { return _left; }
            set
            {
                if (_left != value)
                {
                    Set(() => Left, ref _left, value);
                }
            }
        }

        private double _top;
        public double Top
        {
            get { return _top; }
            set
            {
                if (_top != value)
                {
                    Set(() => Top, ref _top, value);
                }
            }
        }



        /// <summary>
        /// Initializes a new instance of the ViewModelDownloadProgress class.
        /// </summary>
        public ViewModelDownloadProgress()
        {
            this.DownloadPercent = 0;
            Messenger.Default.Register<NotificationMessage<bool>>(this, msg =>
                {
                    switch (msg.Notification)
                    {
                        case "ShowDownloadProgressWindow":
                            this.ShowDownloadProgress = msg.Content;
                            break;
                    }
                });
            Messenger.Default.Register<NotificationMessage<int>>(this, msg =>
                {
                    switch (msg.Notification)
                    {
                        case "SetDownloadProgress":
                            this.DownloadPercent = msg.Content;
                            break;
                        case "SetDownloadErrorCode":
                            this.ErrorCode = msg.Content;
                            break;
                    }
                });


            Messenger.Default.Register<NotificationMessage<Point>>(this, msg =>
                {
                    switch (msg.Notification)
                    {
                        case "UpdateDownloadProgressPosition":
                            UpdatePositionCommand.Execute(msg.Content);
                            break;
                    }
                });

            this.UpdatePositionCommand = new RelayCommand<Point>(UpdatePositionCommandAction);
        }

        private void UpdatePositionCommandAction(Point p)
        {
            this.Left = p.X;
            this.Top = p.Y;
        }
    }
}